package gw.com.android.ui.account;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import www.com.library.view.TintImageView;

/**
 * 登录账号列表适配器
 *
 * @author Administrator
 */
public class LoginHistroyAdapter extends RecyclerView.Adapter<LoginHistroyAdapter.HistroyHolder> {

    private LayoutInflater mInflater;
    private List<String> mList;
    private OnItemListener mListener;

    LoginHistroyAdapter(Context context) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public String getItem(int position) {
        if (null == mList || position < 0 || position > mList.size() - 1) {
            return null;
        }
        return mList.get(position);
    }

    /**
     * 返回集合
     */
    public List<String> getData() {
        return mList;
    }

    /**
     * 为适配器替换新的数据对象
     *
     * @param newList 新数据集合
     */
    void replaceData(List<String> newList) {
        mList = newList;
    }

    /**
     * 删除指定位置的数据
     */
    void removeItem(String item) {
        if (null != mList && mList.size() > 0) {
            mList.remove(item);
        }
    }

    /**
     * 设置监听删除账号回调接口对象
     */
    void setOnItemListener(OnItemListener listener) {
        this.mListener = listener;
    }

    @Override
    public HistroyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new HistroyHolder(mInflater.inflate(R.layout.list_item_account_item, parent, false));
    }

    @Override
    public void onBindViewHolder(HistroyHolder holder, final int position) {
        final String item = getItem(position);

        holder.label.setText(item);
        holder.icon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                mListener.onDeleteItem(item);
            }
        });
        holder.itemView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(item);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (null == mList) {
            return 0;
        }
        return mList.size();
    }

    class HistroyHolder extends RecyclerView.ViewHolder {

        View view;
        TextView label;
        TintImageView icon;

        HistroyHolder(View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.account_layout);
            label = (TextView) itemView.findViewById(R.id.account_delete_text);
            icon = (TintImageView) itemView.findViewById(R.id.delete_item_image);
        }

    }

    public interface OnItemListener {

        void onItemClick(String item);

        void onDeleteItem(String item);

    }

}
